/*
*	Copyright (C) 2014  Julio Montes, Mario Carrillo
*
*	Julio Montes e-mail: imc.coder@gmail.com
*
*	Mario Carrillo e-mail: mario_90ago@live.com.mx
*
*	BSD 3-Clause License
*/

#include "alliance/include/FileCreator.h"


namespace alliance
{
const int Values_States[24][36]
{
//         0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 35 35
    //    A  B  C  D  E  F  G  H	I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  1  . " " (  )  :  ;  _ "-" ,
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//0*
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//1*
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//2*
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//3*
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 0, 0, 0, 0, 0, 0},//4*
    { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,-1, 5,-1,-1,-1,-1, 6, 6,-1},//5*
    { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7,-1,-1, 8,-1, 6,-1, 5},//6*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 7,-1,-1, 8,-1,-1,-1, 5},//7*
    { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,-1,-1, 8,-1,-1,-1,-1, 9, 9,-1},//8*
    { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,-1,10,-1,-1,-1,-1, 9, 9,-1},//9*
    {11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,-1,-1,10,-1,-1,-1,-1,11,11,-1},//10*
    {11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,16,13,-1,15,11,11,-1},//11*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,12,16,13,-1,15,-1,-1,-1},//12*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,13,-1,-1,-1,14,-1,-1,-1},//13*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},//14**
    { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,-1,-1,15,-1,-1,-1,-1,-1,-1,-1},//15*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,17,-1,16,-1,-1,-1,-1,-1,-1,-1},//16*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,17,-1,18,-1,-1,-1,-1,-1,-1,-1},//17*
    {19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,18,-1,-1,-1,-1,19,19,-1},//18*
    {19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,20,-1,-1,-1,-1,19,19,-1},//19*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,21,-1,20,-1,-1,-1,-1,-1,-1,-1},//20*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,21,-1,22,-1,23,-1,-1,-1,-1,-1},//21*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,22,-1,23,-1,-1,-1,-1,-1},//22*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,23,-1,13,-1,15,-1,-1,-1}//23*
};



FileCreator::FileCreator(const wxString& title,char fileNameIn[],char fileNameOut[],int typeFile):wxDialog(NULL,wxID_ANY,title,wxDefaultPosition,wxSize(330,430))
{
    Connect(ID_OK2,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(FileCreator::OnOk));
    Connect(ID_CANCEL2,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(FileCreator::OnCancel));
    m_errors=false;
    m_fileOutFC=fileNameOut;
    m_TF=typeFile;
    AnalyzerFile(fileNameIn);

    if(typeFile)
    {
        ShowInterfaceValuesFileIOC();
    }

    else
    {
        ShowInterfaceValuesFilePAT();
    }

}


void FileCreator::OnOk(wxCommandEvent& event )
{
    if(m_TF)
    {
        CreatFileIOC(m_fileOutFC);
    }

    else
    {
        CreatFilePAT(m_fileOutFC);
    }

    EndModal(wxID_OK);
}


string FileCreator::ConvertionBinary(int num,int limit)
{
    string l_binary = "";
    int l_mask = 1;
    for(int i = 0; i <limit; i++)
    {
        if((l_mask&num) >= 1)
            l_binary = "1"+l_binary;
        else
            l_binary = "0"+l_binary;
        l_mask<<=1;
    }

    return l_binary;
}


bool FileCreator::GetErrorFileCreation()
{
    return m_errors;
}


void FileCreator::CreatFilePAT(char FileNameOutPAT[])
{

    unsigned int i,j,k,l;
    int l_firstIte=1;
    unsigned int l_Max=0;
    int l_NodoMax=0;
    wxString l_St=m_startTime->GetValue();
    wxString l_It=m_increTime->GetValue();
    wxString l_Ut=m_unitTime->GetValue();
    wxString l_Re=m_repetition->GetValue();
    string l_Stime=string(l_St.mb_str());
    string l_Itime=string(l_It.mb_str());
    string l_UTime=string(l_Ut.mb_str());
    unsigned int l_NumRepetition=wxAtoi(l_Re);
    ofstream write_file(FileNameOutPAT);

    for(i=0,j=0; i<m_v.size(); i++)
    {
        write_file<<m_v[i].ModeInOut<<"\t\t"<<m_v[i].NameInOut<<" ";

        if(m_v[i].SizeIntInOut>1)
        {
            write_file<<m_vectorInOuts[j];
            j++;
        }
        write_file<<" B;;;"<<endl;
    }

    write_file<<"IN       vss B;;\nIN       vdd B;;"<<endl;
    write_file<<"\n\n"<<"begin\n\n"<<"-- Pattern description :"<<"\n"<<"--";

    for(i=0,j=0; i<m_v.size(); i++)
    {
        m_v[i].Acum=0;
        if(m_v[i].SizeIntInOut>l_Max)
        {
            l_Max=m_v[i].SizeIntInOut;
            l_NodoMax=i;
        }
        write_file<<m_v[i].NameInOut<<"  ";
    }

    l_Max=pow(2,l_Max);
    i=0;

    while(m_v[l_NodoMax].Acum < l_Max)
    {
        for(j=0; j<l_NumRepetition; j++)
        {
            if(l_firstIte)
            {
                write_file<<"\n< "<<l_Stime<<l_UTime<<">: ";
                l_firstIte=0;
            }
            else
            {
                write_file<<"< +"<<l_Itime<<l_UTime<<">: ";
            }

            for(k=0; k<m_v.size(); k++)
            {
                wxString WXincrIte=(m_vectorValueSignalPAT[k])->GetValue();

                if(m_v[k].ModeInOut=="IN" || m_v[k].ModeInOut=="in")
                {
                    string l_res=ConvertionBinary(m_v[k].Acum,m_v[k].SizeIntInOut);
                    write_file<<l_res<<"\t";
                }
                else
                {
                    write_file<<"?";
                    for(l=0; l<m_v[k].SizeIntInOut; l++)
                    {
                        write_file<<"*";
                    }
                    write_file<<"\t";
                }

                if(j==l_NumRepetition-1)
                {
                    int l_IncINT=wxAtoi(WXincrIte);
                    m_v[k].Acum=m_v[k].Acum+l_IncINT;
                }
            }
            write_file<<"0 1;";
            write_file<<endl;
        }
    }

    write_file<<"\nend;"<<endl;
    write_file.close();
}




void FileCreator::CreatFileIOC(char FileNameOutIOC[])
{

    unsigned int i;
    unsigned int j;
    unsigned int l_FlagTop=0,l_FlagBotton=0,l_FlagLeft=0,l_FlagRight=0,l_FlagIgnore=0;
    ofstream write_file(FileNameOutIOC);

    for(i=0; i<5; i++)
    {
        for(j=0; j<m_vectorCheck.size(); j++)
        {
            wxString l_res;
            l_res=(m_vectorCheck[j])->GetValue();
            string l_PositionInOut =string(l_res.mb_str());

            switch(i)
            {
            case 0:
                if(l_PositionInOut=="TOP")
                {

                    if(l_FlagTop==0)
                    {
                        l_FlagTop=1;
                        write_file<<"\nTOP ( # IOs are ordered from left to right"<<endl;
                    }
                    WriteFileIOC(&write_file,j,i,l_FlagTop);
                }
                break;

            case 1:
                if(l_FlagTop==1)
                {
                    l_FlagTop=0;
                    write_file<<"\n);\n\n";
                }

                if(l_PositionInOut=="BOTTOM")
                {

                    if(l_FlagBotton==0)
                    {
                        l_FlagBotton=1;
                        write_file<<"BOTTOM ( # IOs are ordered from left to right"<<endl;
                    }
                    WriteFileIOC(&write_file,j,i,l_FlagTop);
                }

                break;
            case 2:
                if(l_FlagBotton==1)
                {
                    l_FlagBotton=0;
                    write_file<<"\n);\n\n";
                }
                if(l_PositionInOut=="LEFT")
                {

                    if(l_FlagLeft==0)
                    {
                        l_FlagLeft=1;
                        write_file<<"LEFT ( # IOs are ordered from left to right"<<endl;
                    }
                    WriteFileIOC(&write_file,j,i,l_FlagBotton);
                }

                break;
            case 3:
                if(l_FlagLeft==1)
                {
                    l_FlagLeft=0;
                    write_file<<"\n);\n\n";
                }
                if(l_PositionInOut=="RIGHT")
                {

                    if(l_FlagRight==0)
                    {
                        l_FlagRight=1;
                        write_file<<"RIGHT ( # IOs are ordered from left to right"<<endl;
                    }
                    WriteFileIOC(&write_file,j,i,l_FlagLeft);
                }

                break;
            case 4:
                if(l_FlagRight==1)
                {
                    l_FlagRight=0;
                    write_file<<"\n);\n\n";
                }
                if(l_PositionInOut=="IGNORE")
                {

                    if(l_FlagIgnore==0)
                    {
                        l_FlagIgnore=1;
                        write_file<<"IGNORE ( # IOs are ordered from left to right"<<endl;
                    }
                    WriteFileIOC(&write_file,j,i,l_FlagRight);
                }
                break;
            }
        }
    }
    if(l_FlagIgnore==1)
    {
        l_FlagIgnore=0;
        write_file<<"\n);\n\n";
    }
    write_file.close();
}


void FileCreator::ShowInterfaceValuesFileIOC()
{
    wxPanel *Panel=new wxPanel(this);
    if(m_v.size()==0)
    {
        m_errors=true;
        m_labelIncrease=new wxStaticText(Panel,wxID_ANY,wxT("There aren't enough information\n         for create a ioc file"),wxPoint(50,200),wxDefaultSize);
        m_buttonCancel=new wxButton(Panel,ID_CANCEL2, wxT("OK"),wxPoint(115,360));
        return;
    }

    m_position_Values.Add(L"TOP");
    m_position_Values.Add(L"BOTTOM");
    m_position_Values.Add(L"LEFT");
    m_position_Values.Add(L"RIGHT");
    m_position_Values.Add(L"IGNORE");
    unsigned int i=0;

    m_labelT=new wxStaticText(Panel,wxID_ANY,wxT("Order I/O"),wxPoint(130,10),wxDefaultSize);
    m_labelPosition=new wxStaticText(Panel,wxID_ANY,wxT("Position"),wxPoint(50,60),wxDefaultSize);
    m_labelName=new wxStaticText(Panel,wxID_ANY,wxT("Name"),wxPoint(150,60),wxDefaultSize);
    m_labelType=new wxStaticText(Panel,wxID_ANY,wxT("Type"),wxPoint(225,60),wxDefaultSize);
    m_areaValues=new wxScrolledWindow(Panel,wxID_ANY,wxPoint(35,100),wxSize(250,220),wxVSCROLL,wxT("RESULT"));
    wxBoxSizer *box=new wxBoxSizer(wxVERTICAL);
    m_buttonCancel=new wxButton(Panel,ID_CANCEL2, wxT("Cancel"),wxPoint(50,340));
    m_buttonOk=new wxButton(Panel,ID_OK2, wxT("Ok"),wxPoint(180,340));

    for (i=0; i<m_v.size(); i++)
    {
        wxBoxSizer *BoxH =new wxBoxSizer(wxHORIZONTAL);
        m_vectorCheck.push_back( new wxComboBox(m_areaValues,wxID_ANY,wxT("TOP"),wxDefaultPosition, wxSize(110,30),m_position_Values,wxCB_READONLY));
        BoxH->Add(m_vectorCheck.back());
        BoxH->Add(new wxStaticText(m_areaValues,wxID_ANY,wxT(""),wxDefaultPosition,wxSize(30,30)));
        wxString NameWX(m_v[i].NameInOut.c_str(), wxConvUTF8);
        m_vectorInOut.push_back(new wxStaticText(m_areaValues,wxID_ANY,NameWX,wxDefaultPosition,wxSize(60,30)));
        BoxH->Add(m_vectorInOut.back());
        wxString ModeWX(m_v[i].ModeInOut.c_str(), wxConvUTF8);
        m_vectorInOut.push_back(new wxStaticText(m_areaValues,wxID_ANY,ModeWX,wxDefaultPosition,wxDefaultSize));
        BoxH->Add(m_vectorInOut.back());
        box->Add(BoxH);
    }

    m_areaValues->SetSizer(box);
    m_areaValues->FitInside();
    m_areaValues->SetScrollRate(50,50);

}


void FileCreator::ShowInterfaceValuesFilePAT()
{
    wxPanel *Panel=new wxPanel(this);

    if(m_v.size()==0)
    {
        m_errors=true;
        m_labelIncrease=new wxStaticText(Panel,wxID_ANY,wxT("There aren't enough information\n         for create a pat file"),wxPoint(50,200),wxDefaultSize);
        m_buttonCancel=new wxButton(Panel,ID_CANCEL2, wxT("OK"),wxPoint(115,360));
        return;
    }
    unsigned int i=0;

    wxTextValidator Num_validator(wxFILTER_NUMERIC);
    m_labelTimeStart=new wxStaticText(Panel,wxID_ANY,wxT("Start Time"),wxPoint(30,10),wxDefaultSize);
    m_labelTimeIncr=new wxStaticText(Panel,wxID_ANY,wxT("Increase Time"),wxPoint(30,40),wxDefaultSize);
    m_labelTimeUnit=new wxStaticText(Panel,wxID_ANY,wxT("Unit Time"),wxPoint(30,70),wxDefaultSize);
    m_labelRepetition=new wxStaticText(Panel,wxID_ANY,wxT("Value Repetition"),wxPoint(30,100),wxDefaultSize);
    m_startTime=new wxTextCtrl(Panel,wxID_ANY,wxT("0"),wxPoint(140,5),wxSize(60,25),0,Num_validator);
    m_increTime=new wxTextCtrl(Panel,wxID_ANY,wxT("10"),wxPoint(140,35),wxSize(60,25),0,Num_validator);
    m_unitTime=new wxTextCtrl(Panel,wxID_ANY,wxT("ns"),wxPoint(140,65),wxSize(60,25));
    m_repetition=new wxTextCtrl(Panel,wxID_ANY,wxT("2"),wxPoint(140,95),wxSize(60,25),0,Num_validator);
    m_labelIncrease=new wxStaticText(Panel,wxID_ANY,wxT("Increase"),wxPoint(30,130),wxDefaultSize);
    m_labelName=new wxStaticText(Panel,wxID_ANY,wxT("Name"),wxPoint(90,130),wxDefaultSize);
    m_labelType=new wxStaticText(Panel,wxID_ANY,wxT("Type"),wxPoint(155,130),wxDefaultSize);
    m_areaValues=new wxScrolledWindow(Panel,wxID_ANY,wxPoint(35,160),wxSize(250,190),wxVSCROLL,wxT("RESULT"));
    wxBoxSizer *box=new wxBoxSizer(wxVERTICAL);
    m_buttonCancel=new wxButton(Panel,ID_CANCEL2, wxT("Cancel"),wxPoint(50,360));
    m_buttonOk=new wxButton(Panel,ID_OK2, wxT("Ok"),wxPoint(180,360));

    for (i=0; i<m_v.size(); i++)
    {
        wxBoxSizer *BoxH =new wxBoxSizer(wxHORIZONTAL);
        m_vectorValueSignalPAT.push_back(new wxTextCtrl(m_areaValues,wxID_ANY,wxT("1"),wxDefaultPosition,wxSize(60,30),0,Num_validator));
        BoxH->Add(m_vectorValueSignalPAT.back());
        wxString NameWX(m_v[i].NameInOut.c_str(), wxConvUTF8);
        m_vectorInOut.push_back(new wxStaticText(m_areaValues,wxID_ANY,NameWX,wxDefaultPosition,wxSize(60,30)));
        BoxH->Add(m_vectorInOut.back());
        wxString ModeWX(m_v[i].ModeInOut.c_str(), wxConvUTF8);
        m_vectorInOut.push_back(new wxStaticText(m_areaValues,wxID_ANY,ModeWX,wxDefaultPosition,wxDefaultSize));
        BoxH->Add(m_vectorInOut.back());
        box->Add(BoxH);
    }

    m_areaValues->SetSizer(box);
    m_areaValues->FitInside();
    m_areaValues->SetScrollRate(50,50);
}





void FileCreator::AnalyzerFile(char FileNameIn[])
{

    string l_Name="";
    string l_Mode="";
    vector <string> l_InOutSameType;
    char l_fchar;
    int l_FlagNewRegister=0;
    int l_FlagSameType=0;
    int l_FlagSizeVector=0;
    int l_State=0;
    i=0;
    int l_SizeInt=1;
    m_equalsSize=0;
    string l_V1;
    string l_V2;
    string l_NameVector;
    ifstream read_file(FileNameIn);

    while(!read_file.eof())
    {
        read_file.get(l_fchar);

        if(l_State==-1)
        {
            break;
        }

        switch(l_fchar)
        {
        case 'A':
        case 'a':
            l_State=Values_States[l_State][0];
            break;

        case 'B':
        case 'b':
            l_State=Values_States[l_State][1];
            break;

        case 'C':
        case 'c':
            l_State=Values_States[l_State][2];
            break;

        case 'D':
        case 'd':
            l_State=Values_States[l_State][3];
            break;

        case 'E':
        case 'e':
            l_State=Values_States[l_State][4];
            break;

        case 'F':
        case 'f':
            l_State=Values_States[l_State][5];
            break;

        case 'G':
        case 'g':
            l_State=Values_States[l_State][6];
            break;

        case 'H':
        case 'h':
            l_State=Values_States[l_State][7];
            break;

        case 'I':
        case 'i':
            l_State=Values_States[l_State][8];
            break;

        case 'J':
        case 'j':
            l_State=Values_States[l_State][9];
            break;

        case 'K':
        case 'k':
            l_State=Values_States[l_State][10];
            break;

        case 'L':
        case 'l':
            l_State=Values_States[l_State][11];
            break;

        case 'M':
        case 'm':
            l_State=Values_States[l_State][12];
            break;

        case 'N':
        case 'n':
            l_State=Values_States[l_State][13];
            break;

        case 'O':
        case 'o':
            l_State=Values_States[l_State][14];
            break;

        case 'P':
        case 'p':
            l_State=Values_States[l_State][15];
            break;

        case 'Q':
        case 'q':
            l_State=Values_States[l_State][16];
            break;

        case 'R':
        case 'r':
            l_State=Values_States[l_State][17];
            break;

        case 'S':
        case 's':
            l_State=Values_States[l_State][18];
            break;

        case 'T':
        case 't':
            l_State=Values_States[l_State][19];
            break;

        case 'U':
        case 'u':
            l_State=Values_States[l_State][20];
            break;

        case 'V':
        case 'v':
            l_State=Values_States[l_State][21];
            break;

        case 'W':
        case 'w':
            l_State=Values_States[l_State][22];
            break;

        case 'X':
        case 'x':
            l_State=Values_States[l_State][23];
            break;

        case 'Y':
        case 'y':
            l_State=Values_States[l_State][24];
            break;

        case 'Z':
        case 'z':
            l_State=Values_States[l_State][25];
            break;

        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case'6':
        case '7':
        case '8':
        case '9'://26
            l_State=Values_States[l_State][26];
            break;

        case '.'://27
            l_State=Values_States[l_State][27];
            break;

        case '\n':
        case '\t':
        case ' '://28
            l_State=Values_States[l_State][28];
            break;

        case '('://29
            l_State=Values_States[l_State][29];
            break;

        case ')'://30
            l_State=Values_States[l_State][30];
            break;

        case ':'://31
            l_State=Values_States[l_State][31];
            break;

        case ';'://32
            l_State=Values_States[l_State][32];
            break;

        case '_'://33
            l_State=Values_States[l_State][33];
            break;

        case '-'://34
            l_State=Values_States[l_State][34];
            break;

        case ','://35
            l_State=Values_States[l_State][35];
            break;

        default:
            break;

        }//End de states


        switch(l_State)
        {
        case 0:
            break;

        case 1:
            break;

        case 2:
            break;

        case 3:
            break;

        case 4:
            break;

        case 5:
            if(l_fchar==',')
            {
                l_FlagSameType=1;
                m_equalsSize=m_equalsSize+1;
                l_InOutSameType.push_back(l_Name);
                l_Name="";
            }
            break;

        case 6:

            if(l_FlagNewRegister==1)
            {
                l_FlagNewRegister=0;
                //.................................

                if(l_FlagSizeVector==1)
                {
                    MakeCastingSize(&l_V1,&l_V2,&l_SizeInt);

                    l_FlagSizeVector=0;

                }//fin del if FlagSizeVector


                if(l_FlagSameType==1)
                {
                    l_InOutSameType.push_back(l_Name);
                    l_Name="";

                    l_FlagSameType=0;

                    for(i=0; i<l_InOutSameType.size(); i++)
                    {

                        m_node.NameInOut=l_InOutSameType[i];
                        m_node.ModeInOut=l_Mode;
                        m_node.SizeIntInOut=l_SizeInt;
                        m_v.push_back(m_node);
                    }
                    l_InOutSameType.erase(l_InOutSameType.begin(),l_InOutSameType.end());
                    l_Mode="";
                    l_SizeInt=1;
                    l_V1="";
                    l_V2="";

                }

                else
                {
                    m_node.NameInOut=l_Name;
                    m_node.ModeInOut=l_Mode;
                    m_node.SizeIntInOut=l_SizeInt;
                    l_Name="";
                    l_Mode="";
                    l_SizeInt=1;
                    l_V1="";
                    l_V2="";
                    m_v.push_back(m_node);
                    l_FlagNewRegister=0;
                }
            }

            l_Name+=l_fchar;
            break;

        case 7:
            break;

        case 8:
            break;

        case 9:
            l_Mode+=l_fchar;
            break;

        case 10:
            break;

        case 11:
            break;

        case 12:

            break;

        case 13:

            break;

        case 14:
            //.................................

            if(l_FlagSizeVector==1)
            {
                MakeCastingSize(&l_V1,&l_V2,&l_SizeInt);

                l_FlagSizeVector=0;

            }//fin del if FlagSizeVector

            m_node.NameInOut=l_Name;
            m_node.SizeIntInOut=l_SizeInt;
            m_node.ModeInOut=l_Mode;
            l_Name="";
            l_Mode="";
            l_SizeInt=1;
            l_V1="";
            l_V2="";
            m_v.push_back(m_node);
            break;

        case 15:
            l_FlagNewRegister=1;
            break;

        case 16:
            l_NameVector+=l_fchar;
            break;

        case 17:
            l_FlagSizeVector=1;
            l_NameVector+=l_fchar;
            l_V1+=l_fchar;
            break;

        case 18:
            l_NameVector+=l_fchar;
            break;

        case 19:
            l_NameVector+=l_fchar;
            break;

        case 20:
            l_NameVector+=l_fchar;
            break;


        case 21:
            l_NameVector+=l_fchar;
            l_V2+=l_fchar;
            break;

        case 22:
            l_NameVector+=l_fchar;
            break;

        case 23:

            l_NameVector+=l_fchar;
            for(i=0; i<=m_equalsSize; i++)
            {
                m_vectorInOuts.push_back(l_NameVector);
            }
            l_NameVector="";
            m_equalsSize=0;
            break;

        default:
            break;

        }

    }

    read_file.close();
}



void FileCreator::WriteFileIOC(ofstream *WF,int j,int i,int Flag)
{
    int l_BeginSize;
    l_BeginSize=m_v[j].SizeIntInOut-1;
    string l_NumberPosition;

    if(m_v[j].SizeIntInOut>1)
    {
        while(l_BeginSize>=0)
        {
            stringstream l_Midle;
            l_Midle<<l_BeginSize;
            l_NumberPosition=l_Midle.str();
            *WF<< "     IOPIN "<<m_v[j].NameInOut<<"("<<l_NumberPosition<<").0"<<endl;
            l_BeginSize=l_BeginSize-1;
        }
    }

    else
    {
        *WF<< "     IOPIN "<<m_v[j].NameInOut<<".0"<<endl;
    }
}


void FileCreator::OnCancel(wxCommandEvent& event)
{
    m_errors=true;
    EndModal(wxID_OK);
}


void FileCreator::MakeCastingSize(string *v1,string *v2,int *SI)
{
    string l_vx1=*v1;
    string l_vx2=*v2;
    int l_aux1=atoi(l_vx1.c_str());
    int l_aux2=atoi(l_vx2.c_str());


    if(l_aux1>l_aux2)
    {
        l_aux1=(l_aux2-l_aux1*-1)+1;
        *SI=l_aux1;
    }

    else
    {
        l_aux1=(l_aux2-l_aux1*-1)+1;
        *SI=l_aux1;
    }
}

FileCreator::~FileCreator()
{

}
}





